package com.baikeyang.app.controller.admin.activity;

import com.baikeyang.common.bean.PageResult;
import com.baikeyang.common.bean.ResponseResult;
import com.baikeyang.common.bean.activity.SimpleActivityInfo;
import com.baikeyang.common.constant.ExecuteConstants;
import com.baikeyang.common.model.ActivityInfo;
import com.baikeyang.common.model.User;
import com.baikeyang.common.util.StringUtils;
import com.baikeyang.service.activity.IActivityInfoService;
import com.baikeyang.service.activity.IActivityTypeService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 活动管理
 */
@RestController(value = "activityController")
@RequestMapping(value = "/admin/activity")
public class ActivityController {

    @Autowired
    private IActivityInfoService activityInfoService;

    @RequestMapping(value = "index")
    public ModelAndView index () {
        return new ModelAndView("/admin/activity/activity-index");
    }

    @RequestMapping(value = "list")
    public Object list(String page, String recPerPage) {
        PageResult pagers = new PageResult();
        try{
            Integer size = 10;
            Integer current = 1;
            if(StringUtils.isNotEmpty(recPerPage)){
                try {
                    size = Integer.parseInt(recPerPage);
                } catch (Exception e) {}
            }
            if(StringUtils.isNotEmpty(page)) {
                try {
                    current = Integer.parseInt(page);
                    if(current < 1){current = 1;}
                } catch (Exception e) {}
            }
            Page<ActivityInfo> pageParam = new Page<>(current, size);
            IPage<SimpleActivityInfo> pageResult = activityInfoService.getSimpleActivityInfoPage(pageParam);
            pagers.setData(pageResult.getRecords());
            pagers.setPager(current, pageResult.getTotal());
            pagers.setResult("success");
        }catch (Exception e) {
            e.printStackTrace();
        }
        return pagers;
    }

    @RequestMapping(value = "selectSite")
    public ModelAndView selectSite (String selectType) {
        if(StringUtils.isEmpty(selectType)){
            selectType = "";
        }
        return new ModelAndView("/admin/activity/activity-select-site-map").addObject("selectType", selectType);
    }

    /**
     * 发布活动
     * @return
     */
    @RequestMapping(value = "add")
    public ModelAndView add () {
        return new ModelAndView("/admin/activity/activity-add");
    }

    @RequestMapping(value = "push", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseResult push (ActivityInfo activityInfo) {
        System.out.println(activityInfo);
        return activityInfoService.saveActivityInfo(activityInfo);
        //return new ResponseResult(ExecuteConstants.EXECUTESTATUS.成功, ExecuteConstants.EXECUTEINFO.成功, ExecuteConstants.EXECUTECODE.CODE_20000);
    }

    /**
     * 编辑发布活动
     * @return
     */
    @RequestMapping(value = "edit")
    public ModelAndView edit (String id) {
        return new ModelAndView("/admin/activity/activity-edit").addObject("activity", activityInfoService.getActivityInfoById(id).getData());
    }

    /**
     * 更新发布活动
     * @return
     */
    @RequestMapping(value = "update")
    public ResponseResult update (ActivityInfo activityInfo) {
        return activityInfoService.updateActivityInfo(activityInfo);
    }


}
